#!/usr/bin/perl
#
# cookie based svn user auth
#
use CGI;
use MIME::Base64;
use SvnUser;

my $q=CGI->new;
$q->header(-charset=>'utf-8');

$cookie=$q->cookie('svnadmin');
$id=$q->param('id');
$pw=$q->param('pw');


if(($id ne "")&&($pw ne "")){
	$pwd=SvnUser::get_pwd($id);
	if(($pwd ne "")&&($pw eq $pwd)){
		$auth=$q->cookie(
			-name=>'svnadmin',
			-value=>encode_base64("$id:$pwd",''),
			-path=>'/');
		print $q->header(-cookie=>$auth,'Location'=>'./svnuser.cgi');
		exit 0;
	}
}


if($cookie eq ""){
$login_form=<<"EOF";
<html>
	<head>
		<title>SvnUser Login</title>
		<style type="text/css">
			body{background-color:#93B2D7;}
			input{margin-left:5em;}
			#submit input{height:2em;margin:0;}
			#submit{text-align:right;}
			#login{width:400px;margin:auto;padding:0.8em;border:2px solid #305474;}
			#login span{display:block;margin-top:0.6em;}
		</style>
	</head>
	<body>
		<div id="login">
		<h4>SvnUser Login</h4>
		<form action="#" method="post">
			<span>UserName</span><input type="text" name="id" /><br />
			<span>Password</span><input type="password" name="pw" /><br />
			<div id="submit">
				<input id="submit" type="submit" value="Login" />
			</div>
		<form>
		</div>
	</body>
</html>
EOF
	print $q->header();
	print $login_form;
}else{
	print $q->redirect(-url=>'./svnuser.cgi');
}
